﻿using System;
using System.IO;
using System.Linq;

namespace BaiduIndex.Class
{
    public abstract class ApplicationSet
    {
        public const string IndexUrl = "https://index.baidu.com/api/SearchApi/index?area=0&word=[[%7B%22name%22:%22{0}%22,%22wordType%22:1%7D]]&days=30";
        public const string CheckUrl = "https://data.chinaz.com/keyword/allindex/{0}/index";

        #region MongoDB

        public const string MongoDbUrl = "mongodb://192.168.11.252:27017/?authSource=admin";
        public const string DatabaseName = "baidu-index";
        public const string CookieTable = "cookies";
        public const string PricesTable = "prices";
        public const string RequestsTable = "requests";

        #endregion MongoDB
    }

    public class CookieStore
    {
        public static string GetCookie
        {
            get
            {
                try
                {
                    var cookieTxt = System.Web.HttpContext.Current.Request.PhysicalApplicationPath + "cookie.txt";
                    using (var sr = new StreamReader(cookieTxt))
                    {
                        var cookie = string.Empty;

                        // 从文件读取并显示行，直到文件的末尾
                        while (!sr.EndOfStream)
                        {
                            var str = sr.ReadLine();
                            cookie += str + "\n";
                        }

                        var array = cookie.Split(new[] { '\n' }, StringSplitOptions.RemoveEmptyEntries).ToArray();
                        var index = new Random().Next(0, array.Length);
                        return array[index];
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                    throw;
                }
            }
        }
    }


    public class CipherStore
    {
        public static string GetCipher
        {
            get
            {
                try
                {
                    var cipherTxt = System.Web.HttpContext.Current.Request.PhysicalApplicationPath + "cipher.txt";
                    using (var sr = new StreamReader(cipherTxt))
                    {
                        var cipher = string.Empty;

                        // 从文件读取并显示行，直到文件的末尾
                        while (!sr.EndOfStream)
                        {
                            var str = sr.ReadLine();
                            cipher += str + "\n";
                        }

                        var array = cipher.Split(new[] { '\n' }, StringSplitOptions.RemoveEmptyEntries).ToArray();
                        var index = new Random().Next(0, array.Length);
                        return array[index];
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                    throw;
                }
            }
        }
    }
}